package com.xbts.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.CnHomeProduct;
import com.xbts.system.service.ICnHomeProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xbts.system.mapper.CnHomeProductTypeMapper;
import com.xbts.system.domain.CnHomeProductType;
import com.xbts.system.service.ICnHomeProductTypeService;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;

/**
 * 首页产品类型Service业务层处理
 * 
 * @author xbts
 * @date 2023-11-30
 */
@Service
public class CnHomeProductTypeServiceImpl  extends ServiceImpl<CnHomeProductTypeMapper, CnHomeProductType> implements ICnHomeProductTypeService {

    @Autowired
    private ICnHomeProductService cnHomeProductService;

    /**
     * 查询首页产品类型列表
     *
     * @param cnHomeProductType 首页产品类型
     * @return 首页产品类型
     */
    @Override
    public List<CnHomeProductType> selectCnHomeProductTypeList(CnHomeProductType cnHomeProductType) {
        QueryWrapper<CnHomeProductType> queryWrapper = cnHomeProductType.queryWrapper();
        if (!SecurityUtils.isManager()) {
            queryWrapper.eq("status", 1);
        }

        return list(queryWrapper);
    }

    /**
     * 查询首页产品类型
     *
     * @param id 首页产品类型主键
     * @return 首页产品类型
     */
    @Override
    public CnHomeProductType selectCnHomeProductTypeById(String id) {
        return getById(id);
    }

    /**
     * 新增首页产品类型
     *
     * @param cnHomeProductType 首页产品类型
     * @return 结果
     */
    @Override
    public boolean insertCnHomeProductType(CnHomeProductType cnHomeProductType) {
        cnHomeProductType.setCreateBy(SecurityUtils.getUserId().toString());
        String parentId = cnHomeProductType.getParentId();
        // 判断父级是否存在
        if(StringUtils.isNotEmpty(parentId)){
            CnHomeProductType productType = this.getById(parentId);
            if(productType == null){
                throw new ServiceException("父级产品类型不存在");
            }
            // 判断父级是否有子级
            if(!productType.getHaveChildren().equals("1")){
                productType.setHaveChildren("1");
                this.updateById(productType);
            }
        }

        return save(cnHomeProductType);
    }

    /**
     * 修改首页产品类型
     *
     * @param cnHomeProductType 首页产品类型
     * @return 结果
     */
    @Override
    public boolean updateCnHomeProductType(CnHomeProductType cnHomeProductType) {
        cnHomeProductType.setUpdateBy(SecurityUtils.getUserId().toString());
        cnHomeProductType.setUpdateTime(DateUtils.getNowDate());
        return updateById(cnHomeProductType);
    }

    /**
     * 批量删除首页产品类型
     *
     * @param ids 需要删除的首页产品类型主键
     * @return 结果
     */
    @Override
    public boolean deleteCnHomeProductTypeByIds(List<String> ids) {
        long count = cnHomeProductService.count(new LambdaQueryWrapper<CnHomeProduct>().in(CnHomeProduct::getTypeId, ids));
        if(count > 0){
            throw new ServiceException("删除失败，请先删除首页产品");
        }
        long childrenCount = this.count(new LambdaQueryWrapper<CnHomeProductType>().in(CnHomeProductType::getParentId, ids));
        if(childrenCount > 0){
            throw new ServiceException("删除失败，请先删除子级");
        }
        removeBatchByIds(ids);
        // 获取所有子级
        List<CnHomeProductType> list = list(new LambdaQueryWrapper<CnHomeProductType>().ne(CnHomeProductType::getParentId, "0"));
        List<String> parentIds = list.stream().map(CnHomeProductType::getParentId).distinct().collect(Collectors.toList());
        // 将不存在子级的父级置0
        update(new LambdaUpdateWrapper<CnHomeProductType>().notIn(CnHomeProductType::getId, parentIds).set(CnHomeProductType::getHaveChildren, "0"));
        return true;
    }

    /**
     * 删除首页产品类型信息
     *
     * @param id 首页产品类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteCnHomeProductTypeById(String id) {
        long count = cnHomeProductService.count(new LambdaQueryWrapper<CnHomeProduct>().eq(CnHomeProduct::getTypeId, id));
        if(count > 0){
            throw new ServiceException("删除失败，请先删除首页产品");
        }
      return removeById(id);
    }

}
